package com.example.administrator.financialmanager;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.financialmanager.dao.UserDao;
import com.example.administrator.financialmanager.model.Tb_user;

public class SystemSettingActivity extends AppCompatActivity {

    private EditText sys_set_password_input;
    private Button sys_set_cancel_btn,sys_set_submit_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_setting);

        sys_set_password_input = findViewById(R.id.sys_set_password_input);
        sys_set_cancel_btn = findViewById(R.id.sys_set_cancel_btn);
        sys_set_submit_btn = findViewById(R.id.sys_set_submit_btn);

        sys_set_submit_btn.setOnClickListener(new View.OnClickListener() {// 为设置按钮添加监听事件
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if ("".equals(MainActivity.username)) {
                    sys_set_password_input.setText("");// 清空密码文本框
                    sys_set_password_input.setHint("当前程序以调试状态运行，暂无用户信息");// 为密码文本框设置提示
                } else {
                    UserDao userDao = new UserDao(SystemSettingActivity.this);// 创建PwdDAO对象
                    Tb_user tb_pwd = new Tb_user(MainActivity.username,sys_set_password_input.getText().toString());// 根据输入的密码创建Tb_pwd对象
                    if (userDao.getCount() == 0) {// 判断数据库中是否已经设置了密码
                        userDao.add(tb_pwd);// 添加用户密码
                    } else {
                        userDao.update(tb_pwd);// 修改用户密码
                    }
                    // 弹出信息提示
                    Toast.makeText(SystemSettingActivity.this, "〖密码〗设置成功！", Toast.LENGTH_SHORT)
                            .show();

                    finish();
                }
            }

        });
        sys_set_cancel_btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                sys_set_password_input.setText("");// 清空密码文本框
                sys_set_password_input.setHint("请输入密码");// 为密码文本框设置提示
            }
        });

    }
}
